package com.example.lunch;

import org.apache.activemq.command.ActiveMQTextMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

import javax.jms.*;

/**
 * @ClassName AppLunch
 * @Description
 * @Author Jingxiao.Ye
 * @Date 2022/1/30 下午4:26
 * @Version V1.0
 * @Since JDK 11
 */
@Component
public class AppLunch implements ApplicationRunner {

    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private Queue queue;

    @Autowired
    private Topic topic;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        jmsTemplate.send(queue, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                ActiveMQTextMessage activeMQTextMessage = new ActiveMQTextMessage();
                activeMQTextMessage.setText("hello");
                return activeMQTextMessage;
            }
        });
        jmsTemplate.send(topic, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                ActiveMQTextMessage activeMQTextMessage = new ActiveMQTextMessage();
                activeMQTextMessage.setText("hello1");
                return activeMQTextMessage;
            }
        });
    }
}
